<?php
require_once 'base_model.php';

class Shift_model extends Base_model {
    var $therapist_id;
    var $therapist_name;
    var $date;

    function __construct() {
		parent::__construct('shifts');
	}

	function select_therapists($date) {
	    $this->db->select('therapists.*');
	    $this->db->join('therapists', 'shifts.therapist_id = therapists.id');
	    $this->db->where('shifts.del', 0);
	    $this->db->where('therapists.del', 0);
	    $this->db->where('date', $date);
	    $this->db->order_by('date', 'asc');
		$query = $this->db->get($this->table);
		return $query->result();
	}
    function select($date) {
        $this->db->select('shifts.*, therapists.name as therapist_name');
        $this->db->join('therapists', 'shifts.therapist_id = therapists.id');
        $this->db->where('shifts.del', 0);
        $this->db->where('therapists.del', 0);
        $this->db->where('date', $date);
        $this->db->order_by('date', 'asc');
		$query = $this->db->get($this->table);
		return $query->result();
	}
	function delete_on_date($date) {
		$this->db->where("date", date("Y-m-d", $date));
		$this->db->delete('shifts');
	}
	function insert_all($date, $ids) {
		foreach ($ids as $id) {
			$data = array(
				"therapist_id" => $id,
				"date" => date("Y-m-d", $date),
				"created_date" => date("Y-m-d H:i:s", time()),
				"modified_date" => date("Y-m-d H:i:s", time()),
			);
			$this->db->insert('shifts', $data);
		}
	}
	 function load_from_post($update_create_date = FALSE, $update_modified_date = FALSE) {
		parent::load_from_post($update_create_date);
		$this->do_load_from_post(array('therapist_id', 'date'));
	}
}
